package jmine.tec.environment.db.server;

import java.io.File;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;

import jmine.tec.persist.schema.api.SqlDialect;
import jmine.tec.persist.schema.impl.dialects.DB2Dialect;
import bancosys.tec.utils.db.cleaner.DB2DbCleaner;
import bancosys.tec.utils.db.converter.DbConverter;
import bancosys.tec.utils.db.converter.OracleToDB2Converter;
import bancosys.tec.utils.db.executor.DB2DbExecutor;
import bancosys.tec.utils.db.executor.DbExecutor;

/**
 * TestDBServer para DB2.
 */
public class DB2TestDBServer extends AbstractTestDBServer {

    /**
     * {@inheritDoc}
     */
    public void clearDBData(String... skipTables) throws SQLException {
        Connection conn = null;
        try {
            conn = this.getConnection();
            DB2DbCleaner cleaner = new DB2DbCleaner(conn, this.getDefaultSchema());
            cleaner.clean();
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    public void dropTables() throws SQLException {
        Connection conn = null;
        try {
            conn = this.getConnection();
            DB2DbCleaner cleaner = new DB2DbCleaner(conn, this.getDefaultSchema());
            cleaner.drop();
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
    }

    /**
     * @return O DbExecutor que deve ser utilizado oara este DbServer
     */
    public DbExecutor getDbExecutor() {
        return new DB2DbExecutor();
    }

    /**
     * @param inputFile O arquivo com o schema de criação do banco
     * @param outputFile O arquivo convertido
     * @param dbName O nome do banco
     * @return o DbConverter
     */
    public DbConverter getDbConverter(URL inputFile, File outputFile, String dbName) {
        return new OracleToDB2Converter(inputFile, outputFile, dbName);
    }

    /**
     * {@inheritDoc}
     */
    public SqlDialect getDialect() {
        SqlDialect dialect = new DB2Dialect();
        dialect.setForceSequenceSupport(false);
        return dialect;
    }

}
